package Service;


import Dao.CustomerCompanyDao;
import Dto.CustomerCompany;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CustomerCompanyService {
    private CustomerCompanyDao customerCompanyDao = new CustomerCompanyDao();

    public Map<String, Object> selectCustomerCompanyByPage(Integer pageNum) throws SQLException {
        List<CustomerCompany> list = customerCompanyDao.selectCustomerCompanyList(pageNum);
        Long totalCount = customerCompanyDao.selectCustomerCompanyCount();
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        return map;
    }

    //公司添加
    public void addCustomerCompany(CustomerCompany customerCompany) throws SQLException {
        customerCompanyDao.addCustomerCompany(customerCompany);
    }

    //公司预修改
    public CustomerCompany preUpdateCustomerCompany(Integer id) throws SQLException {
        return customerCompanyDao.preUpdateCustomerCompany(id);
    }

    //公司修改
    public void updateCustomerCompany(CustomerCompany customerCompany) throws SQLException {
        customerCompanyDao.updateCustomerCompany(customerCompany);
    }

    //公司删除
    public void deleteCustomerCompany(Integer id) throws SQLException {
        customerCompanyDao.deleteCustomerCompany(id);
    }

    //两种模糊查询
    public Map<String, Object> selectAllCustomerCompany(String name, String work) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        if (name != null && !name.equals("")) {
            List<CustomerCompany> list = customerCompanyDao.selectByName(name);
            Long totalCount = Long.valueOf(list.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", list);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        if (work != null && !work.equals("")) {
            List<CustomerCompany> list = customerCompanyDao.selectByWork(work);
            Long totalCount = Long.valueOf(list.size());
            Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
            map.put("list", list);
            map.put("totalCount", totalCount);
            map.put("totalPageNum", totalPageNum);
            return map;
        }
        List<CustomerCompany> list = customerCompanyDao.selectAll();
        Long totalCount = customerCompanyDao.selectCustomerCompanyCount();
        Long totalPageNum = totalCount == 0 ? 1 : (totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1);
        map.put("totalCount", totalCount);
        map.put("totalPageNum", totalPageNum);
        map.put("list", list);
        return map;
    }
}